const Websocket = require("ws")
const https = require("https")
const fs = require("fs")
const mediaSoup = require('mediasoup')
const Room = require("./room")
const {RequestController} = require("./controller/requestController")
const Statistics = require("./controller/statisticsController")
const Util = require("./util")
const Message = require("./message")
const RoomMap = require("./roomMap")
const logger = require("./logger")
const {Member} = require('./member')
const config = require('./config')
const {StatisticsController} = require("./controller/statisticsController");

const wsRooms = new Map();
const routerRooms = new Map();
const transportRooms = new Map();
const produceRoom = new Map();
const consumeRoom = new Map();
var rtpCapabilities;

async function run() {

    const roomMap = new RoomMap();
    const statisticsController =  new StatisticsController(roomMap)
    statisticsController.stats()
    await roomMap.init()
    const options = {
        port: 8888,
            options: {
                path: "/roomId/:roomId"
            },
        // cert: fs.readFileSync("/home/fuweicong/work/mediasoup-server/certs/www.duxingzhe.top.pem"),
        // key: fs.readFileSync("/home/fuweicong/work/mediasoup-server/certs/www.duxingzhe.top.key")
    }
    const server = https.createServer(options)
   // const wss = new Websocket.Server({server})

    server.listen(4443, ()=> {
        logger.info("4443 listen")
    })

    const wss = new Websocket.Server({
        port: 8888,
        options: {
            path: "/roomId/:roomId"
        }
    })
    wss.on('error', (error)=> {
        logger.info("ws error", error)
    })

    wss.on('open', async (ws, req)=> {
        console.log("open")
    })
    wss.on('connection', async (ws, req) => {
        new RequestController(ws, req, roomMap)
    })
}

run()
